package eu.dominicum.ft.ui.web.jsf.view.bean;

import eu.dominicum.ft.pd.Participant;
import eu.dominicum.ft.pd.Person;
import eu.dominicum.ft.pd.boundary.FantasyTourManagement;
import eu.dominicum.ft.pd.boundary.PersonManagement;
import eu.dominicum.ft.pd.mao.MakeParticipantMAO;
import org.apache.myfaces.trinidad.context.RequestContext;

import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Backing bean for the MakeParticipantDialog view.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@ManagedBean(name = "makeParticipantDialog")
@ViewScoped
public final class MakeParticipantDialogBean {

    /**
     * {@link FantasyTourManagement}.
     */
    @Inject
    private FantasyTourManagement fantasyTourManagement;

    /**
     * {@link MakeParticipantMAO}.
     */
    @Inject
    private MakeParticipantMAO makeParticipantMAO;

    /**
     * Person select items.
     */
    private List<SelectItem> personSelectItems;

    /**
     * A reference to the Person boundary.
     */
    @Inject
    private PersonManagement personManagement;

    /**
     * Default constructor.
     */
    public MakeParticipantDialogBean() {

    }

    /**
     * Post construct initialiser.
     */
    @PostConstruct
    public void init() {

        List<Person> persons = this.personManagement.findAllPersons();
        if (persons != null) {
            this.personSelectItems = new ArrayList<>(persons.size());
            for (Person person : persons) {
                this.personSelectItems.add(new SelectItem(person.getId(), person.getOrderedName()));
            }
        }
    }

    /**
     * Event handler for the Maak-buttonclick.
     *
     * @param actionEvent -
     */
    public void makeParticipant(final ActionEvent actionEvent) {

        try {
            Long fantasyTourId = (Long) RequestContext.getCurrentInstance().getPageFlowScope().get("fantasyTourId");
            this.getMakeParticipantMAO().setFantasyTourId(fantasyTourId);
            Participant participant = this.fantasyTourManagement.makeParticipant(this.getMakeParticipantMAO());
            RequestContext.getCurrentInstance().returnFromDialog(participant, null);
        } catch (EJBException eex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    eex.getMessage(), null));
        }
    }

    /**
     * .
     *
     * @param newMakeParticipantMAO -
     */
    public void setMakeParticipantMAO(final MakeParticipantMAO newMakeParticipantMAO) {
        this.makeParticipantMAO = newMakeParticipantMAO;
    }

    /**
     * .
     *
     * @return MakeParticipantMAO
     */
    public MakeParticipantMAO getMakeParticipantMAO() {
        return makeParticipantMAO;
    }

    /**
     * .
     *
     * @param newPersonSelectItems -
     */
    public void setPersonSelectItems(final List<SelectItem> newPersonSelectItems) {
        this.personSelectItems = newPersonSelectItems;
    }

    /**
     * .
     *
     * @return List<SelectItem>
     */
    public List<SelectItem> getPersonSelectItems() {
        return personSelectItems;
    }
}
